// Angular Version 6 below need to be deleted {providedIn: 'root'}

// angular
import {Injectable} from '@angular/core';
import {HttpParams} from '@angular/common/http';
// owner
import {Request} from 'pengesoft-ng-lib';
// models
import {Message} from '../domains/message.domain';
import {MessageList} from '../domains/messageList.domain';

/**
 *
 */
@Injectable({
  providedIn: 'root'
})
export class MessageServiceSvr {
  private readonly baseUrl: string;

  constructor(
    private request: Request
  ) {
    this.baseUrl = 'messageServiceSvr.assx/';
  }


  listMessages(token: string, title: string, state: number, receiverId: string, skip: number, size: number): Promise<MessageList> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('title', title.toString())
      .append('state', state.toString())
      .append('receiverId', receiverId.toString())
      .append('skip', skip.toString())
      .append('size', size.toString());
    return this.request.post<MessageList>(this.baseUrl + 'listMessages', httpParams).then((res) => {
      return res;
    });
  }


  updateMessageStatus(token: string, messageId: string): Promise<number> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('messageId', messageId.toString());
    return this.request.post<number>(this.baseUrl + 'updateMessageStatus', httpParams).then((res) => {
      return res;
    });
  }


  GetVersion(): Promise<string> {
    const httpParams = new HttpParams();
    return this.request.post<string>(this.baseUrl + 'GetVersion', httpParams).then((res) => {
      return res;
    });
  }


  deleteMessage(token: string, ids: string[]): Promise<number> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('ids', ids.toString());
    return this.request.post<number>(this.baseUrl + 'deleteMessage', httpParams).then((res) => {
      return res;
    });
  }


  getMessageByPrimaryKey(token: string, id: string): Promise<Message> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('id', id.toString());
    return this.request.post<Message>(this.baseUrl + 'getMessageByPrimaryKey', httpParams).then((res) => {
      return new Message(res);
    });
  }
}
